package com.guaju.yueshangLife.fragment;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.LoginActivity;
import com.guaju.yueshangLife.activity.MainActivity;
import com.guaju.yueshangLife.activity.RefundListActivity;
import com.guaju.yueshangLife.activity.VipCenterActivity;
import com.guaju.yueshangLife.activity.mine.MyAddressesActivity;
import com.guaju.yueshangLife.activity.OrderListActivity;
import com.guaju.yueshangLife.activity.mine.AboutActivity;
import com.guaju.yueshangLife.activity.mine.AdviceActivity;
import com.guaju.yueshangLife.activity.mine.MyCouponActivity;
import com.guaju.yueshangLife.activity.mine.MyFavActivity;
import com.guaju.yueshangLife.activity.mine.MyPocketActivity;
import com.guaju.yueshangLife.activity.mine.ServiceActivity;
import com.guaju.yueshangLife.activity.setting.SettingActivity;
import com.guaju.yueshangLife.base.BaseFragment;
import com.guaju.yueshangLife.bean.Avatar;
import com.guaju.yueshangLife.bean.LoginInfoBean;
import com.guaju.yueshangLife.bean.MeBean;
import com.guaju.yueshangLife.bean.MePermissionBean;
import com.guaju.yueshangLife.databinding.FragmentMineBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.PreferenceUtil;
import com.guaju.yueshangLife.utils.ScreenUtils;
import com.guaju.yueshangLife.widget.BottomShareDialog;
import com.guaju.yueshangLife.widget.mine.ItemMineCate;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


public class MineFragment extends BaseFragment<FragmentMineBinding> {

    private MainActivity activity;

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public int setContent() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        showContentView();
        initListener();
        activity = (MainActivity) getActivity();
    }


    @Override
    protected void loadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        activity.setTab_position(3);
        if (PreferenceUtil.getIsLogin()) {
            ApiEntity.me();
            bindingView.tvUnlogin.setVisibility(View.GONE);
            bindingView.ivSetting.setVisibility(View.VISIBLE);
        } else {
            bindingView.tvName.setText("未登录");
            bindingView.tvUnlogin.setVisibility(View.VISIBLE);
            bindingView.llSupervip.setVisibility(View.GONE);
            bindingView.llVip.setVisibility(View.GONE);
            bindingView.ivSetting.setVisibility(View.GONE);
            bindingView.ivAvatar.setImageURI("");
        }
    }

    private void initListener() {
        setLoginListener();
        setCateListener(bindingView.cateTotal);
        setCateListener(bindingView.cateUnpay);
        setCateListener(bindingView.cateUnreceived);
        setCateListener(bindingView.cateUncommend);
        setCateListener(bindingView.cateRefund);
        bindingView.ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                Intent intent = new Intent(getActivity(), RefundRequestActivity.class);
                Intent intent = new Intent(getActivity(), SettingActivity.class);
//                Intent intent = new Intent(getActivity(), RefundRequestActivity.class);
//                Intent intent = new Intent(getActivity(), SelectRefundTypeActivity.class);
                getActivity().startActivity(intent);
            }
        });
        bindingView.mineAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyAddressesActivity.class);
                startActivity(intent);
            }
        });

        bindingView.otherAdvice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AdviceActivity.class);
                getActivity().startActivity(intent);
            }
        });
        bindingView.otherAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                getActivity().startActivity(intent);
            }
        });

        bindingView.otherService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ServiceActivity.class);
                getActivity().startActivity(intent);
            }
        });
        bindingView.otherShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomShareDialog shareDialog = BottomShareDialog.getInstance(getActivity(), "月上生活", "月山生活，遇上更好的生活!", "", "");
                int screenWidth = ScreenUtils.getScreenWidth(getActivity());
                shareDialog.setWidth(screenWidth);
                shareDialog.show();
            }
        });
        bindingView.minePocket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    startActivity(new Intent(getActivity(), MyPocketActivity.class));

                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bindingView.mineFav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    Toast.makeText(activity, "待开发。。。", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), MyFavActivity.class));

                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bindingView.mineAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    Toast.makeText(activity, "待开发。。。", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), MyAddressesActivity.class));
                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bindingView.mineCuppon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    Toast.makeText(activity, "待开发。。。", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), MyCouponActivity.class));
                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bindingView.mineFapiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    Toast.makeText(activity, "待开发。。。", Toast.LENGTH_SHORT).show();
//                    startActivity(new Intent(getActivity(),));
                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bindingView.mineVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    startActivity(new Intent(getActivity(), VipCenterActivity.class));
                } else {
                    Toast.makeText(activity, "请先登录~", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void setLoginListener() {
        View.OnClickListener loginClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //登录
                if (PreferenceUtil.getIsLogin()) {

                } else {
                    login(v);
                }
            }
        };

        bindingView.ivAvatar.setOnClickListener(loginClickListener);
        bindingView.llLoginInfo.setOnClickListener(loginClickListener);
    }

    private void setCateListener(final ItemMineCate cate) {
        cate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getIsLogin()) {
                    Bundle bundle = new Bundle();
                    if (cate == bindingView.cateTotal) {
                        bundle.putString(OrderListActivity.TABTYPE, OrderListActivity.TAB_TOTAL);
                    } else if (cate == bindingView.cateUnpay) {
                        bundle.putString(OrderListActivity.TABTYPE, OrderListActivity.TAB_UNPAY);
                    } else if (cate == bindingView.cateUnreceived) {
                        bundle.putString(OrderListActivity.TABTYPE, OrderListActivity.TAB_UNRECEIVED);
                    } else if (cate == bindingView.cateUncommend) {
                        bundle.putString(OrderListActivity.TABTYPE, OrderListActivity.TAB_UNCOMMENT);
                    } else if (cate == bindingView.cateRefund) {
                        Intent intent = new Intent(getActivity(), RefundListActivity.class);
                        getActivity().startActivity(intent);
                        return;
                    }
                    Intent intent = new Intent(getActivity(), OrderListActivity.class);
                    intent.putExtra(OrderListActivity.TABTYPE, bundle);
                    getActivity().startActivity(intent);
                } else {
                    login(v);
                }
            }
        });
    }

    private void login(View v) {
        Intent intent = new Intent(v.getContext(), LoginActivity.class);
        v.getContext().startActivity(intent);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MeBean bean) {


        if (bean == null) return;
        MeBean.DataBean data = bean.getData();
        if (data == null)
            return;
        String username = data.getUsername();
//            TODO 识别是否是会员或者超级会员
        bindingView.tvUnlogin.setVisibility(View.GONE);
        bindingView.tvName.setText(username);

        int group_id = data.getGroup_id();
        if (0 == group_id) {
            bindingView.llVip.setVisibility(View.VISIBLE);
            bindingView.llSupervip.setVisibility(View.GONE);
        } else if (1 == group_id) {
            bindingView.llVip.setVisibility(View.GONE);
            bindingView.llSupervip.setVisibility(View.VISIBLE);
        }
        MeBean.DataBean.OrdersBean orders = data.getOrders();
        if (orders != null) {
            int un_paid = orders.getUn_paid();
            int un_received = orders.getUn_received();
            int un_reviewed = orders.getUn_reviewed();
            int refund = orders.getRefund();

            bindingView.cateUnpay.setNums(un_paid);
            bindingView.cateRefund.setNums(refund);
            bindingView.cateUnreceived.setNums(un_received);
            bindingView.cateUncommend.setNums(un_reviewed);
        }
        Avatar avatar = data.getAvatar();
        bindingView.ivAvatar.setImageURI(avatar.getHost() + "/" + avatar.getPath());
    }

}
